package org.goldenport.g3

import org.goldenport.g3.messages.Record
import org.goldenport.g3.events.http._ // XXX

/**
 * @since   Apr. 30, 2010
 * @version Jan. 17, 2011
 * @author  ASAMI, Tomoharu
 */
abstract class G3Exception(message: String, uri: String, cause: Option[Exception] = None) extends RuntimeException(message) {
  cause.foreach(initCause(_))

  def toHttpStatusEvent: HttpStatusEvent
}

class G3ApplicationException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

class G3ComponentException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 400
abstract class BadRequestException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
}

class UrlBadRequestException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http400(uri, cause getOrElse this, message)
}

class ValidationBadRequestException(message: String, uri: String, errors: Record,
                                    cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http200(uri, cause getOrElse this, message, errors)
}

// 401
class UnauthorizeException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http401(uri, cause getOrElse this, message)
}

// 402
class PaymentRequiredException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http402(uri, cause getOrElse this, message)
}

// 403
class ForbiddenException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http403(uri, cause getOrElse this, message)
}

// 404
class NotFoundException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http404(uri, cause getOrElse this, message)
}

// 405
class MethodNotAllowedException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 406
class NotAcceptableException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 407
class ProxyAuthenticationRequiredException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 408
class RequestTimeOutException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 409
class ConflictException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 410
class GoneException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 411
class LengthRequiredException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 412
class PreconditionFailedException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 413
class RequestEntityTooLargeException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 414
class RequestURITooLargeException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 415
class UnsupportedMediaTypeException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 500
class InternalServerErrorException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 501
class NotImplementedException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 502
class BadGatewayException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 503
class ServiceUnavailableException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 504
class GatewayTimeOutException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}

// 505
class HTTPVersionNotSupportedException(message: String, uri: String, cause: Option[Exception] = None) extends G3Exception(message, uri, cause) {
  def toHttpStatusEvent: HttpStatusEvent = Http500(uri, cause getOrElse this, message)
}
